package com.reminiscence.main;

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.reminiscence.utils.Utils;

public class Profilo extends Activity implements OnClickListener
{
	TextView txtProfiloNome;
	TextView txtProfiloCognome;
	TextView txtProfiloData;
	EditText edtTitoloStudio;
	EditText edtProfiloInfo;
	EditText edtResidenza;
	EditText edtProfPrinc;
	EditText edtProfAt;
	ImageView imgProfilo;
	Button btnProfiloSalva;
	Button btnProfiloAnnulla;
	
	private int RESULT_LOAD_IMAGE = 1;
	public static Bitmap bitmap;
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profilo);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		Utils.showAlert(this, "Ciao! Grazie per esserti registrato a Reminiscences. Per completare la registrazine puoi inserire in questa pagina ulteriori informazioni su di te. Grazie e buon divertimento!", "", "Benvenuto!");

		MainActivity.utente.leggiUtente(this);
		
		edtProfiloInfo = (EditText)findViewById(R.id.edtProfiloInfo);
		edtTitoloStudio = (EditText)findViewById(R.id.edtTitoloStudio);
		edtResidenza = (EditText)findViewById(R.id.edtResidenza);
		edtProfPrinc = (EditText)findViewById(R.id.edtProfPas);
		edtProfAt = (EditText)findViewById(R.id.edtProfAt);
		txtProfiloNome = (TextView)findViewById(R.id.txtNome);
		txtProfiloCognome = (TextView)findViewById(R.id.txtCognome);
		imgProfilo = (ImageView)findViewById(R.id.imgProfilo);
		btnProfiloAnnulla = (Button)findViewById(R.id.btnProfiloAnnulla);
		btnProfiloSalva = (Button)findViewById(R.id.btnProfiloSalva);
		btnProfiloAnnulla.setOnClickListener(this);
		btnProfiloSalva.setOnClickListener(this);
		
		
		imgProfilo.setClickable(true);
		imgProfilo.setOnClickListener(this);
		
		txtProfiloNome.setText(MainActivity.utente.nome);
		txtProfiloCognome.setText(MainActivity.utente.cognome);
	}

	@Override
	public void onClick(View arg0)
	{
		if (arg0.getId() == R.id.imgProfilo)
		{
			Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(i, RESULT_LOAD_IMAGE);
			
			BitmapDrawable bd = (BitmapDrawable)imgProfilo.getDrawable();
			Bitmap bitmap = bd.getBitmap();
			ByteArrayOutputStream bao = new ByteArrayOutputStream();
			
			byte ba[] = bao.toByteArray();
			MainActivity.utente.immagine = (Base64.encodeToString(ba, Base64.DEFAULT));
		}
		
		if (arg0.getId() == R.id.btnProfiloSalva)
		{
			MainActivity.utente.info = edtProfiloInfo.getText().toString();
			MainActivity.utente.titoloStudio = edtTitoloStudio.getText().toString();
			MainActivity.utente.residenza = edtResidenza.getText().toString();
			MainActivity.utente.profPrinc = edtProfPrinc.getText().toString();
			MainActivity.utente.profAt = edtProfAt.getText().toString();
			MainActivity.utente.aggiornaProfilo(this);
			
			bitmap = ((BitmapDrawable)imgProfilo.getDrawable()).getBitmap();
			
			
			Utils.uploadFoto(this, bitmap, MainActivity.utente.nomeUtente);

			Intent i = new Intent(this, ProfiloCompleto.class);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		}
		if (arg0.getId() == R.id.btnProfiloAnnulla)
		{
			Intent i = new Intent(this, ProfiloCompleto.class);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		}
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
        super.onActivityResult(requestCode, resultCode, data);
         
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
        {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
 
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
 
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
             
            ImageView imageView = (ImageView) findViewById(R.id.imgProfilo);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
	}
}
